﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Public Class DoSinhHieu
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Private flag As Boolean = False 'tránh loop xtragrid
    Private m_MaPhieu As String = Nothing
    Public Sub setMa(ByVal ma As String)
        Me.m_MaPhieu = ma
    End Sub
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        If b Then
            Me.IGridView_TienSu.OptionsBehavior.Editable = False
            Me.IGridView_TienSu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else
            Me.IGridView_TienSu.OptionsBehavior.Editable = True
            Me.IGridView_TienSu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
            Me.BarButtonItemTaoMoi.Enabled = False
            Me.BarButtonItemFirst.Enabled = False
            Me.BarButtonITemPrevious.Enabled = False
            Me.BarButtonItemNext.Enabled = False
            Me.BarButtonItemLast.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
            Me.BarButtonItemTaoMoi.Enabled = True
            Me.BarButtonItemFirst.Enabled = True
            Me.BarButtonITemPrevious.Enabled = True
            Me.BarButtonItemNext.Enabled = True
            Me.BarButtonItemLast.Enabled = True
        End If
        '  setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DoSinhHieuTienSuBenhTableAdapter.Fill(Me.PCV_CLINICDataSet.DoSinhHieuTienSuBenh)
        Me.DoSinhHieuTableAdapter.Fill(Me.PCV_CLINICDataSet.DoSinhHieu)
        Me.DMBenhTableAdapter.Fill(Me.PCV_CLINICDataSet.DMBenh)
        Me.DMBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet.DMBenhNhan)
        setReadonlyControl(True)
        SplashScreenManager.CloseForm()
    End Sub

    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)
        'Mã //set ngầm
        Dim row As DataRowView = CType(DoSinhHieuBindingSource.AddNew(), DataRowView)
        Me.Ma = "DSH" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        'mặc định
        Me.NgaysinhIDateEdit.EditValue = Today
        Me.MasophieuITextEdit.Focus()
        Me.State = 1
    End Sub
    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridView_TienSu.setUpdateData()
            Me.DoSinhHieuBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.DoSinhHieuTienSuBenhBindingSource.EndEdit()
            If Me.PCV_CLINICDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        ' Me.TableAdapterManager1.UpdateAll(Me.PCV_CLINICDataSet)
                        Me.TableAdapterManager1.UpdateAll(Me.PCV_CLINICDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonlyControl(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
                Me.State = 0 'trở về trạng thái mặc định 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try

    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.DoSinhHieuBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonITemPrevious.ItemClick
        Me.DoSinhHieuBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNext.ItemClick
        Me.DoSinhHieuBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLast.ItemClick
        Me.DoSinhHieuBindingSource.MoveLast()
    End Sub

    '' 
    Private Sub BarButtonItemIn_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        Try
            Dim report As rp_DoSinhHieu = New rp_DoSinhHieu
            report.DoSinhHieuTableAdapter1.FillMa_DoSinhHieu(report.PcV_CLINICDataSet1.DoSinhHieu, Me.IGridView2.GetFocusedRowCellValue("ma"))
            report.DoSinhHieuTienSuBenhTableAdapter1.FillMa_DoSinhHieuTienSuBenh(report.PcV_CLINICDataSet1.DoSinhHieuTienSuBenh, Me.IGridView_TienSu.GetFocusedRowCellValue("ma"))
            report.ngaythangnam.Value = "Ngày " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(0, 2) & " tháng " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(3, 2) & " năm " & Me.NgaydoIDateEdit.EditValue.ToString.Substring(6, 4)
            report.ShowRibbonPreview() 'hiển thị report theo kiểu ribbon
        Catch ex As Exception

        End Try

    End Sub

    Private Sub MabenhnhanISearchLookupEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MabenhnhanISearchLookupEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MabenhnhanISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DiachiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
        Me.TuoiITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tuoi")
        Me.GioitinhITextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "gioitinh")
        Me.NgaysinhIDateEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ngaythangnamsinh")
    End Sub

    Private Sub IGridView_TienSu_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridView_TienSu.InitNewRow
        Me.IGridView_TienSu.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridView_TienSu.SetFocusedRowCellValue("sochungtu", Me.MasophieuITextEdit.EditValue)
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit1_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit1.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridView_TienSu.SetFocusedRowCellValue("mabenh", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridView_TienSu.SetFocusedRowCellValue("tenbenh", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        IGridView_TienSu.SetFocusedRowCellValue("ghichu", editor.Properties.View.GetRowCellValue(rowHandle, "ghichu"))
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.MasophieuITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.DoSinhHieuBindingSource.RemoveCurrent()
            Me.DoSinhHieuTableAdapter.Update(Me.PCV_CLINICDataSet.DoSinhHieu)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.DoSinhHieuBindingSource.Position
        Me.State = 0
        loadDuLieu()
        Me.DoSinhHieuBindingSource.Position = position

    End Sub

    Private Sub NhipthoITextEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles NhipthoITextEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup3.SelectedTabPageIndex = 1
            Me.NgaydoIDateEdit1.Focus()
        End If

    End Sub

    Private Sub TinhtrangITextEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangITextEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup3.SelectedTabPageIndex = 2
            Me.DoSinhHieuBindingSource.EndEdit()
            Me.IGridView_TienSu.MoveLast()
            Me.IGridView_TienSu.Focus()
            Me.IGridView_TienSu.AddNewRow()
            Me.IGridView_TienSu.FocusedColumn = Me.IGridView_TienSu.VisibleColumns(0)
            Me.IGridView_TienSu.ShowEditor()
        End If
    End Sub

    Private Sub MabenhnhanISearchLookupEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MabenhnhanISearchLookupEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup3.SelectedTabPageIndex = 0
            Me.NgaydoIDateEdit.EditValue = Today
            Me.NgaydoIDateEdit.Focus()
        End If
    End Sub
End Class
